package gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.swing.JPanel;

public class PicturePanel extends JPanel{
	private BufferedImage image;
	
	public PicturePanel() {
		this.setBackground(Color.GREEN);
		this.setSize(500, 600);
		image = null;
		this.setVisible(true);
	}
	
	public BufferedImage getImage(){
		return image;
	}
	
	public void setImage(File file){
		try {
			image = javax.imageio.ImageIO.read(file);
			this.repaint();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void setImage(BufferedImage img){
		image = img;
		//this.repaint();
	}
	
	protected void paintComponent(Graphics g){	
		g.clearRect(0, 0, this.getWidth(), this.getHeight());
		g.drawLine(0, 0, 0, this.getHeight());
		//resize obrazka
		if(image != null){
			int width, height;
			
			int max, min, maxFrame, minFrame;
			
			width = image.getWidth();
			height = image.getHeight();
			float factor;
			if(image.getWidth()>this.getWidth() || image.getHeight() > this.getHeight()){
				factor = (float)width/(float)height;
				if(width > height){
					width = this.getWidth()-10;
					height = (int) ((int)width/factor);
				}
				else {
					height = this.getHeight()-10;
					width = (int) (height*factor);
				}
				g.drawImage(image, (this.getWidth()-width)/2,(this.getHeight()-height)/2, width, height, null);
			}
			else g.drawImage(image, (this.getWidth()-width)/2,(this.getHeight()-height)/2, null);			
		}
	}
}
